<template>
  <div class="app-container">
    <el-form :model="searchForm" :inline="true">
      <el-form-item label="名称">
        <el-input
          v-model="searchForm.resultOne"
          placeholder="请输入名称"
          @keyup.enter="getSearch"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" size="mini" @click="getSearch"
          >搜索</el-button
        >
      </el-form-item>
      <el-form-item>
        <el-button icon="Refresh" size="mini" @click="cancelSearch"
          >重置</el-button
        >
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['ruoyi-device:modelfloorgroup:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['ruoyi-device:modelfloorgroup:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>
    <el-table
      v-loading="loading"
      :data="listPage"
      @selection-change="handleSelectionChange"
    >
      <el-table-column label="监控组名称" align="center" prop="name" />
      <!-- <el-table-column label="所属单位" align="center" prop="deptName" /> -->
      <!-- <el-table-column label="建筑名称" align="center" prop="buildNameFirst" /> -->
      <!-- <el-table-column label="楼层" align="center" prop="fName" /> -->
      <!-- <el-table-column label="建筑FBX" align="center" prop="bName" /> -->
      <el-table-column label="摄像头数量" align="center" prop="camCount" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        width="250"
      >
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="handleToPage(scope.row)"
            v-hasPermi="['ruoyi-device:modelbuildgroup:edit']"
            ><el-icon><Place /></el-icon>分配设备</el-button
          >
          <el-button
            link
            type="primary"
            icon="Edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['ruoyi-device:modelfloorgroup:edit']"
            >修改</el-button
          >
          <el-button
            link
            type="primary"
            icon="Delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['ruoyi-device:modelfloorgroup:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!-- <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    /> -->
    <el-pagination
      :ref="setRefs"
      align="center"
      background="true"
      @current-change="handleSelectionChanges"
      style="float: right; margin-top: 20px"
      :page-size="pageSize"
      :current-page="pageNum"
      :pager-count="11"
      layout="total,prev, pager, next,jumper"
      :total="total"
    />

    <!-- 添加或修改监控摄像头楼层分组对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form
        ref="modelfloorgroupRef"
        :model="form"
        :rules="rules"
        label-width="80px"
      >
        <el-form-item label="名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入名称" />
        </el-form-item>
        <!-- <el-form-item label="楼层" prop="fId">
          <el-select v-model="form.fId" placeholder="请选择楼层" filterable>
            <el-option
              v-for="item in optionsFloor"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item> -->
        <!-- <el-form-item label="所属单位" prop="deptId">
          <el-tree-select
            v-model="form.deptId"
            :data="optionsDept"
            :props="{
              value: 'deptId',
              label: 'deptName',
              children: 'children',
            }"
            placeholder="请选择单位"
            check-strictly
          />
        </el-form-item> -->
        <!-- <el-form-item label="建筑" prop="bId">
          <el-select v-model="form.bId" placeholder="请选择建筑" filterable>
            <el-option
              v-for="item in optionsBuilding"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item> -->
        <el-form-item label="建筑FBX" prop="bName">
          <el-input v-model="form.bName" placeholder="请输入建筑FBX" />
        </el-form-item>
        <!-- <el-form-item label="摄像头数量" prop="camCount">
          <el-input v-model="form.camCount" placeholder="请输入摄像头数量" />
        </el-form-item> -->
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Modelfloorgroup">
import { onMounted } from "vue";
onMounted(() => {
  getTreeselectDept();
  getArrayFloor();
  getArrayBuilding();
});

import { listDept } from "@/api/system/dept";
const optionsDept = ref([]);
const arrayDept = ref([]);
function getTreeselectDept() {
  let queryParams1 = {
    pageNum: 1,
    pageSize: 10000,
  };
  listDept(queryParams1).then((response) => {
    optionsDept.value = [];
    arrayDept.value = response.data;
    arrayDept.value.forEach((item1) => {
      item1.deptId = item1.deptId * 1;
    });
    optionsDept.value = proxy.handleTree(arrayDept.value, "deptId");
  });
}

import { listFloor } from "@/api/baseInfo/floor";
const arrayFloor = ref([]);
function getArrayFloor() {
  let queryParams1 = {
    pageNum: 1,
    pageSize: 20000,
  };
  listFloor(queryParams1).then((response) => {
    arrayFloor.value = response.rows;
    getOptionsFloor(response.rows);
  });
}
const optionsFloor = ref([]);
function getOptionsFloor(tempList) {
  optionsFloor.value = [];
  tempList.forEach((element) => {
    optionsFloor.value.push({
      label: element.name,
      value: element.id,
    });
  });
}

import { listBuilding } from "@/api/baseInfo/building";
const arrayBuilding = ref([]);
function getArrayBuilding() {
  let queryParams1 = {
    pageNum: 1,
    pageSize: 20000,
  };
  listBuilding(queryParams1).then((response) => {
    arrayBuilding.value = response.rows;
    getOptionsBuilding(response.rows);
  });
}

const optionsBuilding = ref([]);
function getOptionsBuilding(tempList) {
  optionsBuilding.value = [];
  tempList.forEach((element) => {
    optionsBuilding.value.push({
      label: element.name,
      value: element.id,
    });
  });
}

import {
  listModelfloorgroup,
  getModelfloorgroup,
  delModelfloorgroup,
  addModelfloorgroup,
  updateModelfloorgroup,
  getMonitor,
} from "@/api/baseInfo/modelfloorgroup";

const { proxy } = getCurrentInstance();

const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    name: null,
    bName: null,
  },
  rules: {},
});

const { queryParams, form, rules } = toRefs(data);
import { useRoute, useRouter } from "vue-router";
const route = useRoute();
const router = useRouter();
function handleToPage(entity1) {
  router.push({
    path: "hvac",
    query: {
      id: entity1.id,
      name: entity1.name,
      buildNameFirst: entity1.buildNameFirst,
    },
  });
}
/** 查询监控摄像头楼层分组列表 */
const modelfloorgroupList = ref([]);
function getList() {
  loading.value = true;

  getMonitor(route.query.id).then((response) => {
    console.log(response);
    modelfloorgroupList.value = response.data;
    total.value = response.total;
    loading.value = false;
    getPageList(modelfloorgroupList.value);
  });
}

//自定义分页相关
import {
  JSONreflush,
  emptyIsTrue,
  emptyIsFalse,
  removeBracketsAndPipes,
} from "@/api/123wwwww/universalMethod";
const pageNum = ref(1);
const pageSize = ref(10);
const total = ref(10);
const listPage = ref([]);
function handleSelectionChanges(page) {
  pageNum.value = page;
  getTableList();
}
//取得分页数据
function getTableList() {
  //搜索判断
  if (searchFlag.value == 0) {
    getPageList(modelfloorgroupList.value);
  } else if (searchFlag.value == 1) {
    getPageList(searchList.value);
  }
}
function getPageList(tempList) {
  listPage.value = [];
  if (pageNum.value > total.value / pageSize.value) {
    pageNum.value = Math.ceil(total.value / pageSize.value);
  }
  total.value = tempList.length;
  if (!emptyIsTrue(tempList)) {
    for (let i = (pageNum.value - 1) * pageSize.value; i < total.value; i++) {
      listPage.value.push(tempList[i]);
      if (listPage.value.length === pageSize) break;
    }
  }
}

// 搜索相关
const searchFlag = ref(0);
const searchList = ref([]);
const searchForm = ref({});
import { ElMessage } from "element-plus";
//-------------搜索功能--------------
function getSearch() {
  if (JSON.stringify(searchForm.value) !== "{}") {
    searchFlag.value = 1;
    let tempList = [];
    for (let i = 0; i < modelfloorgroupList.value.length; i++) {
      if (
        //模糊查询
        modelfloorgroupList.value[i].name.indexOf(
          searchForm.value.resultOne
        ) !== -1 ||
        emptyIsTrue(searchForm.value.resultOne)
      )
        tempList.push(modelfloorgroupList.value[i]);
    }
    pageNum.value = 1;
    searchList.value = JSONreflush(tempList);
    tempList = [];
    getTableList();
  } else {
    ElMessage({
      message: "搜索条件为空",
      type: "warning",
    });
  }
}
function cancelSearch() {
  pageNum.value = 1;
  searchFlag.value = 0;
  getTableList();
  searchForm.value = {};
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    name: null,
    fId: null,
    deptId: null,
    bId: null,
    bName: null,
    camCount: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    createBy: null,
    remark: null,
    delFlag: null,
  };
  proxy.resetForm("modelfloorgroupRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加监控摄像头楼层分组";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value;
  getModelfloorgroup(_id).then((response) => {
    form.value = response.data;
    open.value = true;
    title.value = "修改监控摄像头楼层分组";
  });
}

/** 提交按钮 */
function submitForm() {
  form.value.bid = Number(route.query.id);
  
  proxy.$refs["modelfloorgroupRef"].validate((valid) => {
    if (valid) {
      if (form.value.id != null) {
        updateModelfloorgroup(form.value).then((response) => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addModelfloorgroup(form.value).then((response) => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal
    .confirm('是否确认删除监控摄像头楼层分组编号为"' + _ids + '"的数据项？')
    .then(function () {
      return delModelfloorgroup(_ids);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download(
    "ruoyi-device/modelfloorgroup/export",
    {
      ...queryParams.value,
    },
    `modelfloorgroup_${new Date().getTime()}.xlsx`
  );
}

getList();
</script>
